package client_gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import actions.ChangeNickAction;

import utils.Constants;
import utils.ImageCreator;

/**
 * Klasa predstavlja Info panel, koji sadrzi avatar, nickname i status logovanog korisnika.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class InfoPanel extends JPanel implements Constants {
	
	
	/**
	 * Vlasnik - glavni prozor aplikacije.
	 */
	private ClientWindow owner;
	
	
	/**
	 * Layout manager - GridBagLayout
	 */
	private GridBagLayout gridBag = null;

	
	// ogranicenja u GridBagLayout-u
	private GridBagConstraints avatarLabelConstraint = null;
	private GridBagConstraints avatarButtonConstraint = null;
	private GridBagConstraints nicknameLabelConstraint = null;
	private GridBagConstraints nicknameFieldConstraint = null;
	private GridBagConstraints nicknameButtonConstraint = null;
	private GridBagConstraints statusLabelConstraint = null;
	private GridBagConstraints statusButtonConstraint = null;
	
	private GridBagConstraints refreshButtonConstraint = null;
	private JButton refreshButton = null;
	
	
	/**
	 * Border.
	 */
	private TitledBorder titledBorder;
	
	
	// komponente ovog panela
	private JLabel avatarLabel;
	private JLabel nicknameLabel;
	private JLabel statusLabel;
	private JButton avatarButton;
	private JTextField nicknameField;
	private ChangeNickAction nicknameChangeButton;
	private JButton statusButton;
		
	

	/**
	 * Konstruktor.
	 * @param owner - glavni prozor aplikacije
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public InfoPanel(ClientWindow owner) {
		this.owner = owner;
		
		this.gridBag = new GridBagLayout();
		
		avatarLabelConstraint = new GridBagConstraints();
		avatarButtonConstraint = new GridBagConstraints();
		nicknameLabelConstraint = new GridBagConstraints();
		nicknameFieldConstraint = new GridBagConstraints();
		nicknameButtonConstraint = new GridBagConstraints();
		statusLabelConstraint = new GridBagConstraints();
		statusButtonConstraint = new GridBagConstraints();
		
		refreshButtonConstraint = new GridBagConstraints();

		
		titledBorder = BorderFactory.createTitledBorder(BorderFactory.
									createEtchedBorder(EtchedBorder.RAISED),
									"Info", javax.swing.border.TitledBorder.CENTER,
									javax.swing.border.TitledBorder.DEFAULT_POSITION,
									null, null);
		
		initializeInfoPanel();
	}


	
	/**
	 * Metod vrsi inicijalizaciju ovog panela.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private void initializeInfoPanel() {
		this.setLayout(gridBag);
		this.setBorder(titledBorder);
		
		// inicijalizacija ogranicenja LayoutManager-a
		initializeConstraints();
		
		// dodavanje komponenti u panel
		this.add(getAvatarLabel());
		this.add(getAvatarButton());
		this.add(getNicknameLabel());
		this.add(getNicknameField());
		this.add(getNicknameChangeButton());
		this.add(getStatusLabel());
		this.add(getStatusButton());
		this.add(getRefreshButton());
	}
	
	
	/**
	 * Metod vrsi inicijalizaciju ogranicenja LayoutManager-a.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private void initializeConstraints() {
		avatarLabelConstraint.gridx = 0;
		avatarLabelConstraint.gridy = 0;
		avatarLabelConstraint.weightx = 0;
		avatarLabelConstraint.weighty = 1;
		avatarLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		avatarLabelConstraint.insets = new Insets(0,10,2,10);
		
		avatarButtonConstraint.gridx = 0;
		avatarButtonConstraint.gridy = 1;
		avatarButtonConstraint.weightx = 0;
		avatarButtonConstraint.weighty = 1;
		avatarButtonConstraint.anchor = GridBagConstraints.NORTHWEST;
		avatarButtonConstraint.insets = new Insets(5,10,5,10);
		
		nicknameLabelConstraint.gridx = 1;
		nicknameLabelConstraint.gridy = 0;
		nicknameLabelConstraint.weightx = 1;
		nicknameLabelConstraint.weighty = 1;
		nicknameLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameLabelConstraint.insets = new Insets(0,5,2,5);
	
		nicknameFieldConstraint.gridx = 1;
		nicknameFieldConstraint.gridy = 1;
		nicknameFieldConstraint.weightx = 1;
		nicknameFieldConstraint.weighty = 1;
		nicknameFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameFieldConstraint.insets = new Insets(4,5,5,5);
		nicknameFieldConstraint.fill = GridBagConstraints.HORIZONTAL;
		
		nicknameButtonConstraint.gridx = 1;
		nicknameButtonConstraint.gridy = 1;
		nicknameButtonConstraint.weightx = 1;
		nicknameButtonConstraint.weighty = 1;
		nicknameButtonConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameButtonConstraint.insets = new Insets(28,5,5,6);
		nicknameButtonConstraint.fill = GridBagConstraints.HORIZONTAL;
		
		statusLabelConstraint.gridx = 2;
		statusLabelConstraint.gridy = 0;
		statusLabelConstraint.weightx = 1;
		statusLabelConstraint.weighty = 1;
		statusLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		statusLabelConstraint.insets = new Insets(0,10,2,10);
		
		statusButtonConstraint.gridx = 2;
		statusButtonConstraint.gridy = 1;
		statusButtonConstraint.weightx = 1;
		statusButtonConstraint.weighty = 1;
		statusButtonConstraint.anchor = GridBagConstraints.NORTHWEST;
		statusButtonConstraint.insets = new Insets(3,10,5,10);
		statusButtonConstraint.fill = GridBagConstraints.HORIZONTAL;
		
		refreshButtonConstraint.gridx = 2;
		refreshButtonConstraint.gridy = 1;
		refreshButtonConstraint.weightx = 1;
		refreshButtonConstraint.weighty = 1;
		refreshButtonConstraint.anchor = GridBagConstraints.NORTHEAST;
		refreshButtonConstraint.insets = new Insets(33,50,5,10);
	}
	
	
	
	// Metode koje inicijalizuju komponente ovog panela.
	
	private JLabel getAvatarLabel() {
		if (avatarLabel == null) {
			avatarLabel = new JLabel("Avatar:");
			gridBag.setConstraints(avatarLabel, avatarLabelConstraint);
		}
		
		return avatarLabel;
	}
	
	
	private JButton getAvatarButton() {
		if (avatarButton == null) {
			avatarButton = new JButton();
			avatarButton.setToolTipText("Promena Avatara");
			avatarButton.setIcon(ImageCreator.createImageIcon(InfoPanel.class, DEFAULT_AVATAR));
			avatarButton.setPreferredSize(new Dimension(50,50));
			avatarButton.setMinimumSize(new Dimension(50,50));
			gridBag.setConstraints(avatarButton, avatarButtonConstraint);
			
			avatarButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					AvatarDialog avatarDialog = new AvatarDialog(owner);
					avatarDialog.setVisible(true);
				}
			});
		}
		
		return avatarButton;
	}
	
	
	private JLabel getNicknameLabel() {
		if (nicknameLabel == null) {
			nicknameLabel = new JLabel("Nickname:");
			gridBag.setConstraints(nicknameLabel, nicknameLabelConstraint);
		}
		
		return nicknameLabel;
	}
	

	private JTextField getNicknameField() {
		if (nicknameField == null) {
			nicknameField = new JTextField();
			nicknameField.setFont(new Font("Verdana", Font.BOLD, 12));
			nicknameField.setEditable(false);
			gridBag.setConstraints(nicknameField, nicknameFieldConstraint);
		}
		
		return nicknameField;
	}
	
	
	private JButton getNicknameChangeButton() {
		if (nicknameChangeButton == null) {
			nicknameChangeButton = new ChangeNickAction(owner);
			nicknameChangeButton.setToolTipText("Promena Nickname-a");
			gridBag.setConstraints(nicknameChangeButton, nicknameButtonConstraint);
		}
		
		return nicknameChangeButton;
	}
	
	
	private JLabel getStatusLabel() {
		if (statusLabel == null) {
			statusLabel = new JLabel("Status:");
			gridBag.setConstraints(statusLabel, statusLabelConstraint);
		}
		
		return statusLabel;
	}
	
	
	
	private JButton getStatusButton() {
		if (statusButton == null) {
			statusButton = new JButton("Status");
			statusButton.setToolTipText("Promena Statusa");
			statusButton.setPreferredSize(new Dimension(80, 25));
			statusButton.setMargin(new Insets(0,2,0,2));
//			statusButton.setBackground(new Color(178, 223, 238));
			gridBag.setConstraints(statusButton, statusButtonConstraint);
			
			statusButton.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					
					int x = 0;
					int y = statusButton.getHeight();
					
					owner.getStatusContexMenu().show((Component)e.getSource(), x, y);
				}
			});
		}
		
		return statusButton;
	}
	
	
	
	// public interface
	
	
	/**
	 * Metod postavlja avatar.
	 * @param avatar - novi avatar
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public void setAvatar(String avatar) {
		avatarButton.setIcon(ImageCreator.createImageIcon(InfoPanel.class, "/avatars/" + avatar + ".png"));
	}
	
	
	/**
	 * Metod postavlja nickname.
	 * @param nickname - novi nickname
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public void setNickname(String nickname) {
		nicknameField.setText(nickname);
	}
	
	
	/**
	 * Metod postavlja status.
	 * @param status - novi status
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public void setStatus(int status) {
		statusButton.setText(STATUS_NAMES[status]);
		statusButton.setIcon(ImageCreator.createImageIcon(InfoPanel.class, "/status_images/" + STATUS_NAMES[status] + ".png"));
	}

	
	
	private JButton getRefreshButton() {
		if (refreshButton == null) {
			refreshButton = new JButton();
			refreshButton.setIcon(ImageCreator.createImageIcon(InfoPanel.class, "/actions/images/refresh.png"));
			refreshButton.setToolTipText("Osvezavanje liste kontakata ukoliko se nepravilno iscrta!");
			refreshButton.setPreferredSize(new Dimension(20,20));
			gridBag.setConstraints(refreshButton, refreshButtonConstraint);
			refreshButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					owner.refreshTree();
				}
			});
		}
		
		return refreshButton;
	}
}
